package com.ha.garbage.adapter;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ha.garbage.R;
import com.ha.garbage.context.BaseContext;
import com.ha.garbage.model.Gift;
import com.ha.garbage.model.News;
import com.ha.garbage.model.OrderDTO;
import com.ha.garbage.model.Result;
import com.ha.garbage.service.OrderService;
import com.ha.garbage.utils.UserRetrofit;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class GiftAdapter extends RecyclerView.Adapter<GiftAdapter.MyHolder>{


   private List<Gift> list = new ArrayList<>();

    public GiftAdapter(List<Gift> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate((R.layout.gift_item), null);
      return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {

        //绑定数据
        Gift gift = list.get(position);
        //设置数据
        holder.tv_create_time.setText(gift.getCreateTime());
        holder.tv_gift_name.setText(gift.getName());
        holder.tv_credit.setText(gift.getCreditPoint().toString());
        holder.tv_number.setText(gift.getNumber().toString());

        String url = gift.getImage();
        Glide.with(holder.imageView.getContext()).load(url).into(holder.imageView);
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(v.getContext())
                        .setTitle("是否将该礼物设置为想要")
                        .setMessage("礼物介绍："+gift.getName())
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                OrderDTO orderDTO = new OrderDTO(BaseContext.getCurrentId(), gift.getId());
                                Retrofit retrofit = UserRetrofit.retrofit();
                                OrderService orderService = retrofit.create(OrderService.class);
                                Call<Result> resultCall = orderService.addOrder(orderDTO);
                                resultCall.enqueue(new Callback<Result>() {
                                    @Override
                                    public void onResponse(Call<Result> call, Response<Result> response) {
                                        if(response.isSuccessful()){
                                            Result result = response.body();
                                            if(result.getCode() == 1){
                                                new AlertDialog.Builder(v.getContext())
                                                        .setTitle("礼物想要")
                                                        .setMessage("已经将该礼物："+gift.getName()+"放入订单列表")
                                                        .setPositiveButton("知道了",null)
                                                        .show();
                                            }else {
                                                new AlertDialog.Builder(v.getContext())
                                                        .setTitle("礼物想要")
                                                        .setMessage("订单生成出错"+result.getMsg())
                                                        .setPositiveButton("知道了",null)
                                                        .show();
                                            }
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<Result> call, Throwable t) {

                                        System.out.println("请求地址为"+call.request().url());
                                        System.out.println("请求失败原因："+t.toString());
                                    }
                                });

                            }
                        })
                        .show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder{

        TextView tv_gift_name;
        TextView tv_credit;
        TextView tv_number;
        TextView tv_create_time;
        ImageView imageView;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            //初始化控件
           tv_credit = itemView.findViewById(R.id.tv_credit);
           tv_number = itemView.findViewById(R.id.tv_number);
           tv_gift_name = itemView.findViewById(R.id.tv_gift_name);
           tv_create_time = itemView.findViewById(R.id.tv_create_time);

            imageView = itemView.findViewById(R.id.img_gift);

        }
    }
}
